/*
 * Author: Qiao Yu
 * Date: 2010.3.11
 * Description: A simple code editor that allows highlighting and multi-coloring.
 */

package sjtu.edu.cn.se.compiler11.gui;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.CaretEvent;
import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.MovementEvent;

public class CodeEditor extends StyledText{
	
	private Shell theShell;
	
	private int lastHighLighted = 0;
	
	public CodeEditor(Composite parent, int style, Shell shell)
	{
		
		super(parent,style);
		theShell = shell;
				
		this.addCaretListener(new CaretListener(){

			@Override
			public void caretMoved(CaretEvent arg0) {
				// TODO Auto-generated method stub
				setHighLight(getCurrentLineNumber() - 1, new Color(theShell.getDisplay(),232,242,254));
				cancelHighLight(lastHighLighted);
				lastHighLighted = getCurrentLineNumber() - 1;
				MainWindow.setLineNumber(getCurrentLineNumber(),getCaretOffset ());
			}
			
		});
			
		/* check if the newly-typed word is a key word */
		this.addExtendedModifyListener(new ExtendedModifyListener(){
		      public void modifyText(ExtendedModifyEvent event) {
		    	  setKeyWordColor();
		    	 
		    	/*  String newText = getTextRange(event.start,event.length);

		    	  if (newText.compareTo(" ") != 0)
		    		  return;
		    	  
		    	  String allText = getText();
		    	  int lastSpace = event.start + event.length - 2;
		    	  while (allText.charAt(lastSpace) != ' ')
		    		  lastSpace--;
		    	  String word = allText.substring(lastSpace+1);
		    	  
		    	  
		    	  for (String keyWord : keyWordList){
		  			int index = word.indexOf(keyWord);
		  			if (index != -1){
		  				Color keyWordColor = theShell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		  				StyleRange styleRange = new StyleRange();
		  				styleRange.start = index;
		  				styleRange.length = keyWord.length();
		  				styleRange.foreground = keyWordColor;
		  				setStyleRange(styleRange);
		  			}
		    	  }*/
		      }
			});
	}
	
	public int getCurrentLineNumber()
	{
		return getLineAtOffset(getCaretOffset()) + 1;
	}
	
	public void setShell(Shell shell)
	{
		
		theShell = shell;
	}
	
	/* highlight a line, using Color to specify a color */
	public void setHighLight(int lineNumber, Color backColor)
	{
		this.setLineBackground(lineNumber,1,backColor);
	}
	
	/* highlight a line, using red*/
	public void setHighLight(int lineNumber)
	{
		this.setLineBackground(lineNumber, 1, theShell.getDisplay().getSystemColor(SWT.COLOR_RED));
	}
	
	/* de-highlight a line */
	public void cancelHighLight(int lineNumber)
	{
		this.setLineBackground(lineNumber,1,theShell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
	}
	
	public void setCommentColor(int lineNumber)
	{
		this.setLineBackground(lineNumber,1,new Color(theShell.getDisplay(),63,127,95));	
	}
	
	/* use another color to emphasize the keywords */
	public void setKeyWordColor()
	{
		
		// cancel keyword multi-coloring
		StyleRange styleRange = new StyleRange();
		styleRange.start = 0;
		styleRange.length = this.getText().length();
		styleRange.foreground = theShell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
		this.setStyleRange(styleRange);
		
		Color keyWordColor = theShell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
		
		// set keyword multi-coloring
		String allText = this.getText();
		for (String keyWord : keyWordList){
			int index = allText.indexOf(keyWord);
			while (index != -1){
				styleRange.start = index;
				styleRange.length = keyWord.length();
				styleRange.foreground = keyWordColor;
				this.setStyleRange(styleRange);
				index = allText.indexOf(keyWord, index + 1);
			}
			
		}
		
		
		
	}	
	

	private static final String[] keyWordList = {"int ","for ","abstract", "boolean",
																"break ", "byte", "case","catch",
																"char", "class", "continue", "default",									
														"do" ,"double", "else" ,"extends" ,"false",
															"final" ,"finally" ,"float","if",
															"implements", "import" ,"instanceof" , "interface",
															"long", "native", "new","null","package",
															"private" ,"protected" ,"public", "return" ,"short",
															"static" ,"super" ,"switch", "synchronized", "this",
															"throw", "throws" ,"transient" ,"true", "try",
															"void" ,"volatile", "while" ,"System.out.println","pre:","post:"};
		


}
